package com.sales.service.impl;

import com.sales.dao.AccountDao;
import com.sales.dao.CommissionDetailsDao;
import com.sales.dao.ProfileDao;
import com.sales.dao.StoreDao;
import com.sales.entity.Account;
import com.sales.entity.CommissionDetails;
import com.sales.entity.Profile;
import com.sales.entity.Store;
import com.sales.enums.ExceptionEnum;
import com.sales.enums.UserTypeEnums;
import com.sales.excption.SalesException;
import com.sales.service.CommissionDetailsService;
import com.sales.signUpReq.ReferralCodeSignUpReq;
import com.sales.util.MyDateutils;
import com.sales.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by lihu on 2017/10/14.
 */
@Service
public class CommissionDetailsServiceImpl implements CommissionDetailsService {

    @Autowired
    ProfileDao profileDao;

    @Autowired
    StoreDao storeDao;

    @Autowired
    CommissionDetailsDao commissionDetailsDao;

    @Autowired
    AccountDao accountDao;


    @Override
    @Transactional
    public void initCode(ReferralCodeSignUpReq referralCode) {
        Profile profile = profileDao.findByReferralCode(referralCode.getReferralCode());
        if (profile == null) {
            throw new SalesException(ExceptionEnum.PROFILE_NOT_EXISTS);
        }
        //修改店铺数量
        profile.setStoreCount(profile.getStoreCount() + 1);
        profileDao.save(profile);
        //如果存在上级
        if (StringUtils.isNotBlank(profile.getParentUserName())) {
            updateStoreCount(profile.getParentUserName());
        }
        Store store = new Store(referralCode.getStoreId(),
                referralCode.getStoreName(),
                referralCode.getReferralCode(),
                MyDateutils.increaseMonth(new Date(), 1));
        storeDao.save(store);
    }

    @Override
    @Transactional
    public void commissionSync(Store store) {
        Profile profile = profileDao.findByReferralCode(store.getCode());
        if (profile == null) {
            throw new SalesException(ExceptionEnum.PROFILE_NOT_EXISTS);
        }
        //一级用户提成800元
        int amount = 80000;
        if (profile.getUserTypeEnums() == UserTypeEnums.ZERO) {
            amount = 120000;
        }

        //如果为二级用户 上级用户需收取提成
        if (profile.getUserTypeEnums() == UserTypeEnums.TWO) {
            //上级用户提成20000；
            int headAmount = 20000;
            amount -= headAmount;
            //帐户变动
            Account account = accountDao.findByUsername(profile.getParentUserName());
            account.addAmount(headAmount);
            accountDao.save(account);
            CommissionDetails commissionDetails = new CommissionDetails(UuidUtil.getUuid(),
                    profile.getParentUserName(), store.getId(), store.getStoreName(), 20000);
            commissionDetailsDao.save(commissionDetails);
        }

        //帐户变动
        Account account = accountDao.findByUsername(profile.getUsername());
        account.addAmount(amount);
        accountDao.save(account);

        CommissionDetails commissionDetails = new CommissionDetails(UuidUtil.getUuid(),
                profile.getUsername(), store.getId(), store.getStoreName(), amount);
        commissionDetailsDao.save(commissionDetails);
    }

    /**
     * 修改上级店铺数量
     *
     * @param username
     */
    private void updateStoreCount(String username) {
        Profile profile = profileDao.findByUsername(username);
        if (profile != null) {
            profile.setStoreCount(profile.getStoreCount() + 1);
            profileDao.save(profile);
            //如果存在上级
            if (StringUtils.isNotBlank(profile.getParentUserName())) {
                updateStoreCount(profile.getParentUserName());
            }
        }
    }
}
